package com.ruoyi.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.CrClub;
import com.ruoyi.manage.service.ICrClubService;
import com.ruoyi.manage.mapper.CrClubMemberMapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 社团信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-20
 */
@RestController
@RequestMapping("/campus/club")
public class CrClubController extends BaseController
{
    @Autowired
    private ICrClubService crClubService;

    @Autowired
    private CrClubMemberMapper crClubMemberMapper;

    /**
     * 查询社团信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CrClub crClub)
    {
        startPage();
        List<CrClub> list = crClubService.selectActiveClubList(crClub);
        return getDataTable(list);
    }

    /**
     * 导出社团信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:club:export')")
    @Log(title = "社团信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrClub crClub)
    {
        List<CrClub> list = crClubService.selectCrClubList(crClub);
        ExcelUtil<CrClub> util = new ExcelUtil<CrClub>(CrClub.class);
        util.exportExcel(response, list, "社团信息数据");
    }

    /**
     * 获取社团信息详细信息
     */
    @GetMapping(value = "/{clubId}")
    public AjaxResult getInfo(@PathVariable("clubId") Long clubId)
    {
        return success(crClubService.selectCrClubByClubId(clubId));
    }

    /**
     * 新增社团信息
     */
    @PreAuthorize("@ss.hasPermi('manage:club:add')")
    @Log(title = "社团信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrClub crClub)
    {
        return toAjax(crClubService.insertCrClub(crClub));
    }

    /**
     * 修改社团信息
     */
    @PreAuthorize("@ss.hasPermi('manage:club:edit')")
    @Log(title = "社团信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrClub crClub)
    {
        return toAjax(crClubService.updateCrClub(crClub));
    }

    /**
     * 删除社团信息
     */
    @PreAuthorize("@ss.hasPermi('manage:club:remove')")
    @Log(title = "社团信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clubIds}")
    public AjaxResult remove(@PathVariable Long[] clubIds)
    {
        return toAjax(crClubService.deleteCrClubByClubIds(clubIds));
    }

    /**
     * 获取热门社团
     */
    @GetMapping("/popular")
    public AjaxResult getPopularClubs()
    {
        List<CrClub> list = crClubService.selectPopularClubs(10);
        return success(list);
    }

    /**
     * 按类型查询社团
     */
    @GetMapping("/type/{clubType}")
    public AjaxResult getClubsByType(@PathVariable("clubType") String clubType)
    {
        List<CrClub> list = crClubService.selectClubListByType(clubType);
        return success(list);
    }

    /**
     * 获取社团统计信息
     */
    @GetMapping("/stats")
    public AjaxResult getClubStats()
    {
        // TODO: 实现统计逻辑
        return success("统计功能开发中");
    }

    /**
     * 获取当前登录用户所属（管理）的社团上下文
     */
    @GetMapping("/context/my")
    public AjaxResult getMyClubContext()
    {
        Long userId = getUserId();
        Long clubId = crClubMemberMapper.selectManagedClubIdByUserId(userId);
        return success(new java.util.HashMap<String, Object>() {{
            put("userId", userId);
            put("clubId", clubId);
        }});
    }
}